import os
import tkinter as tk
from tkinter import *
from tkinter.ttk import *
import pygetwindow as gw
import win32gui
from src.target_type.window_way import window_way
from src.ocr.paddle_ocr import paddle_ocr

class app_window(tk.Tk):

    def __init__(self):
        super().__init__()
        self.cb1_label = None
        self.frame1 = None
        self.canvas = None
        self.btn1 = None
        self.btn2 = None
        self.btn3 = None
        self.resizable(False, False)
        self.title("C挂机助手")
        self.width = "1536"
        self.height = "864"
        self.canvas_1_width = int(self.width) / 2
        self.canvas_1_height = int(self.height) / 2
        self.geometry(str(int(self.width) + 6) + "x" + self.height)
        self.window_titles = None
        self.cb1 = None
        self.cb2 = None
        self.cb3 = None
        self.cb4 = None
        self.cb5 = None
        self.btn_1_text = "刷新"
        self.btn_2_text = "开始挂机"
        self.btn_3_text = "停止挂机"
        self.predict_tool = None
        self.ocr_tool = None
        self.timer_id = None
        self.pic = None
        self.is_cpu = "cpu"
        self.refresh_time = 1000

    def create_window(self):
        self.create_menu()
        self.create_frame_1()
        self.create_comboboxs_1()
        self.create_button_1()
        self.create_button_2()
        self.create_canvas_1()
        self.create_button_3()
        self.create_comboboxs_2()
        self.create_comboboxs_3()
        self.create_comboboxs_4()
        self.create_comboboxs_5()

    def create_menu(self):
        menu_bar = Menu(self)
        file_menu = Menu(menu_bar, tearoff=0)
        file_menu.add_command(label="菜单一", command=lambda: None)  # 这里可以实现保存功能
        file_menu.add_separator()
        file_menu.add_command(label="退出", command=lambda: None)
        menu_bar.add_cascade(label="文件", menu=file_menu)
        about_menu = Menu(menu_bar, tearoff=0)
        about_menu.add_command(label="关于", command=lambda: None)
        about_menu.add_command(label="信息", command=lambda: None)
        menu_bar.add_cascade(label="帮助", menu=about_menu)
        self.config(menu=menu_bar)

    def create_frame_1(self):
        Style().configure('TFrame', background='#DDDDDD')
        self.frame1 = Frame(self, style='TFrame')
        self.frame1.place(x=0, y=0, width=int(self.width) / 2, height=self.height)

    def create_comboboxs_1(self):
        self.window_titles = gw.getAllTitles()
        # 窗口选择框
        values = []
        for index, title in enumerate(self.window_titles):
            if not (title is None or len(title) == 0):
                values.append(title)
        self.cb1 = Combobox(self.frame1, state="readonly")
        self.cb1['values'] = values
        self.cb1.place(x=70, y=0, width=400, height=30)
        Style().configure('TLabel', background='#DDDDDD')
        self.cb1_label = Label(self.frame1, text="窗口选择:", style='TLabel')
        self.cb1_label.place(x=5, y=0, height=30)

    def refresh_comboboxs_1(self, event):
        self.window_titles = gw.getAllTitles()
        # 窗口选择框
        values = []
        for index, title in enumerate(self.window_titles):
            if not (title is None or len(title) == 0):
                values.append(title)
        self.cb1['values'] = values

    def create_button_1(self):
        # 刷新按钮
        self.btn1 = Button(self.frame1, text=self.btn_1_text, takefocus=False, )
        self.btn1.place(x=480, y=0, width=80, height=30)
        self.btn1.bind('<Button-1>', self.refresh_comboboxs_1)

    def create_button_2(self):
        # 开始挂机按钮
        self.btn2 = Button(self.frame1, text=self.btn_2_text, takefocus=False)
        self.btn2.place(x=580, y=0, width=80, height=30)
        self.btn2.bind('<Button-1>', self.start_window_way)
        self.btn2.config(stat='active')

    def create_button_3(self):
        # 停止挂机按钮
        self.btn3 = Button(self.frame1, text=self.btn_3_text, takefocus=False)
        self.btn3.place(x=680, y=0, width=80, height=30)
        self.btn3.config(stat='disabled')

    def start_window_way(self, event):
        self.predict_tool = window_way(title=self.cb1.get(), device_name=self.cb2.get(), model_file_name=self.cb3.get())
        self.ocr_tool = paddle_ocr()
        self.refresh_window_way(event)
        self.btn2.config(stat='disabled')
        self.btn2.unbind_all('<Button-1>')
        self.btn3.config(stat='active')
        self.btn3.bind('<Button-1>', self.stop_window_way)

    def refresh_window_way(self, event):
        global results, image
        results = []
        image = None
        yuanshi_image = self.predict_tool.capture_image()
        v = self.cb4.get()
        if v == "显示预测框":
            results = self.predict_tool.predict_anchor(yuanshi_image)
            image = self.predict_tool.predict_image(yuanshi_image, results)
            if image is not None:
                self.canvas.create_image(0, 0, anchor='nw', image=image)
        elif v == "显示图像":
            image = self.predict_tool.predict_nobox_image(yuanshi_image)
            if image is not None:
                self.canvas.create_image(0, 0, anchor='nw', image=image)
        else:
            results = self.predict_tool.predict_anchor(yuanshi_image)

        if results:
            points = self.predict_tool.jiexi_result(results)
            self.mouse_keyboard_action(points)
        else:
            print("无目标")

        self.ocr_tool.ocr_image(yuanshi_image)
        self.timer_id = self.after(self.refresh_time, self.refresh_window_way, None)

    def stop_window_way(self, event):
        if self.timer_id is not None:
            self.after_cancel(self.timer_id)
            del self.predict_tool
            self.predict_tool = None
        self.btn2.config(stat='active')
        self.btn2.bind('<Button-1>', self.start_window_way)
        self.btn3.config(stat='disabled')
        self.btn3.unbind_all('<Button-1>')

    def create_canvas_1(self):
        self.canvas = tk.Canvas(self, bg='black', width=self.canvas_1_width, height=self.canvas_1_height)  # 绘制画布
        self.canvas.place(x=self.canvas_1_width, y=0)

    def create_comboboxs_2(self):
        self.cb2 = Combobox(self.frame1, state="readonly")
        self.cb2['values'] = ["cpu", "cuda"]
        self.cb2.place(x=5, y=35, width=100, height=30)
        self.cb2.bind('<<ComboboxSelected>>', self.change_comboboxs_2)
        self.cb2.set("cpu")

    def change_comboboxs_2(self, event):
        self.is_cpu = self.cb2.get()

    def create_comboboxs_3(self):
        values = []
        current_path = os.getcwd()
        for root, dirs, files in os.walk(current_path + "/models"):
            for file in files:
                print(file)
                values.append(file)
        self.cb3 = Combobox(self.frame1, state="readonly")
        self.cb3['values'] = values
        self.cb3.place(x=115, y=35, width=100, height=30)
        self.cb3.set("yolov8n.pt")

    def create_comboboxs_4(self):
        values = ["皆不显示", "显示图像", "显示预测框"]
        self.cb4 = Combobox(self.frame1, state="readonly")
        self.cb4['values'] = values
        self.cb4.place(x=225, y=35, width=100, height=30)
        self.cb4.set("皆不显示")

    def create_comboboxs_5(self):
        def check_refresh_time(event):
            self.refresh_time = int(self.cb5.get())

        values = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 120, 150, 200, 300, 500, 1000, 2000, 3000, 5000, 10000]
        self.cb5 = Combobox(self.frame1, state="readonly")
        self.cb5['values'] = values
        self.cb5.place(x=325, y=35, width=100, height=30)
        self.cb5.bind("<<ComboboxSelected>>", check_refresh_time)
        self.cb5.set(self.refresh_time)

    def mouse_keyboard_action(self, points):
        print("\n")
        for point in points:
            print("123")
            print(point.name)


if __name__ == '__main__':
    app = app_window()
    app.create_window()
    app.mainloop()
